$( document ).ready(function() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='appointDocId']").val("");
	$("[name='appointDocFirstname']").val("");
	$("[name='appointDocLastname']").val("");
	$("[name='firstname']").val("");
	$("[name='lastname']").val("");
	/*
	$("[name='year']").val("-1");
	$("[name='month']").val("-1");
	$("[name='day']").val("-1");
	*/
	$("#dateOfBirth").datepicker();
	$("[name='sin']").val("");
	$("[name='ohip']").val("");
	$("[name='gender']").val("-1");
	$("[name='phoneNum']").val("");
	$("[name='cellphone']").val("");
	$("[name='email']").val("");
	$("[name='address']").val("");
	$("[name='postalCode']").val("");
	$("[name='defaultDoctor']").val("");
	$("[name='currentHealth']").val("");
});

function createPatient(){
	$.getJSON("staff/createPatient.html?firstname="+$("[name='firstname']").val()+
			"&lastname="+$("[name='lastname']").val()+
			/*
			"&year="+$("[name='year']").val()+
			"&month="+$("[name='month']").val()+
			"&day="+$("[name='day']").val()+
			*/
			"&date="+$("#dateOfBirth").val()+
			"&sin="+$("[name='sin']").val()+
			"&ohip="+$("[name='ohip']").val()+
			"&gender="+$("[name='gender']").val()+
			"&phoneNum="+$("[name='phoneNum']").val()+
			"&cellphone="+$("[name='cellphone']").val()+
			"&email="+$("[name='email']").val()+
			"&address="+$("[name='address']").val()+
			"&postalCode="+$("[name='postalCode']").val()+
			"&defaultDoctor="+$("[name='defaultDoctor']").val()+
			"&currentHealth="+$("[name='currentHealth']").val(), function(data){
		
		if(data.flag==true){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("success");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
			$("[name='appointDocId']").val("");
			$("[name='appointDocFirstname']").val("");
			$("[name='appointDocLastname']").val("");
			$("[name='firstname']").val("");
			$("[name='lastname']").val("");
			/*
			$("[name='year']").val("-1");
			$("[name='month']").val("-1");
			$("[name='day']").val("-1");
			*/
			$("#dateOfBirth").val("");
			$("[name='sin']").val("");
			$("[name='ohip']").val("");
			$("[name='gender']").val("-1");
			$("[name='phoneNum']").val("");
			$("[name='cellphone']").val("");
			$("[name='email']").val("");
			$("[name='address']").val("");
			$("[name='postalCode']").val("");
			$("[name='defaultDoctor']").val("");
			$("[name='currentHealth']").val("");
		}else if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}
	});
}

function getDocSearchResult(){
	$("#appointDocSearchResult").empty();
	$.getJSON("staff/appointDocSearch.html?appointDocId="+$("[name='appointDocId']").val()+"&appointDocFirstname="+$("[name='appointDocFirstname']").val()+"&appointDocLastname="+$("[name='appointDocLastname']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.appointDocSearchResult)){
			$("#appointDocSearchResult").data("docSearchResult", data);
			printDocTableBody(data);
			$("#appointDocSearchResult").append("<br /><button id=\"confirmDoc\" onclick=\"confirmDoc()\" class=\"button\">confirm</button>&nbsp;");
		}else{
			$("#appointDocSearchResult").append("<strong>No result.</strong>");
		}
		
	});
}

function clearDtable(){
	$("#appointDocSearchResult").empty();
}

function printDocTableBody(data){
	$("#appointDocSearchResult")
	.append(
			"<table id=\"docList\" class=\"tablecss\">" +
			"<thead>" +
			"<tr>" +
			"<th></th>"+
			"<th>Doctor id</th>" +
			"<th>First name</th>" +
			"<th>Last name</th>" +
			"<th>Gender</th>" +
			"<th>Office phone number</th>" +
			"<th>Office room</th>" +
			"<th>Department</th>" +
			"</tr>" +
			"</thead>" +
			"<tbody id=\"docTableBody\">" +
			"</tbody>" +
			"</table>");
var tr;
$(data).each(
	function(index, value) {
		$(value.appointDocSearchResult).each(function (i,v){
			tr = $("<tr>");
			tr.append("<td><input type=\"radio\" name=\"docRadio\" value=\""+v.userId+"\"></td>");
			tr.append("<td>" + v.userId + "</td>");
			tr.append("<td>" + v.firstname + "</td>");
			tr.append("<td>" + v.lastname + "</td>");
			var gen=v.gender;
			if(gen=='m'){
				tr.append("<td>male</td>");
			}else if(gen=='f'){
				tr.append("<td>female</td>");
			}else{
				tr.append("<td></td>");
			}
			tr.append("<td>" +v.officeInfo.officePhone+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.officeRoom+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.deptInfo.deptName+"</td>");
			tr.append("</tr>");
			$("#docTableBody").append(tr);
		});
		
	});
$("#docList").tablesorter({
	widgets : [ 'zebra' ],
	headers : {
		0 : {
			sorter : false
		}
	}
});
}

function confirmDoc(){
	var id=$("[name=\"docRadio\"]:checked").val();
	$("[name=\"defaultDoctor\"]").val(id);
	$("#appointDocSearchResult").empty();
}